<?php
	/* -------------------------------------------------------------------
	IOU - expense sharing system 
	Copyright (C) 2009-2013 - Mischa Spelt (m.spelt@gmail.com)
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	------------------------------------------------------------------- */

	include_once "lib/lib.main.php";
	checklogin();
	include_once "lib/groupinfo.php";

	print_header("Group details: " . $db->q("value SELECT DisplayName FROM Groups WHERE ID = %i", $_REQUEST['id']));
	printGroupInfo($_REQUEST['id']);
	print "<h1>Bills</h1>";
	print "<table class='center'>";
	print "<tr><th style='width: 5em;'></th><th></th><th style='width: 6em;'></th><th style='width: 6em;'></th></tr>";
	$bills = $db->q("table SELECT ID, Description, Date, CreatedBy, IsRepayment, IsPrivate FROM Bills WHERE GroupID = %i ORDER BY Date DESC", $_REQUEST['id']);
	$i = 0;
	foreach($bills as $bill) {
		// If the bill is private and user is not creator or in the journal, do not display
		if($bill['IsPrivate'] && !($bill['CreatedBy'] == $gUser['ID'] || $db->q("value SELECT COUNT(*) FROM Journal WHERE BillID = %i AND UserID = %i", $bill['ID'], $gUser['ID']) > 0)) 
			continue;

		// Assign an access key if possible
		$accessKey = assignAccessKey($bill['Description']);

		// Calculate total amount for which user is involved on this bill
		$myChargeAmount = $db->q("value SELECT SUM(Amount / Divisor) FROM Journal WHERE UserID = %i AND BillID = %i AND IsPayment = 0", $gUser['ID'], $bill['ID']);
		$myContribAmount = $db->q("value SELECT -SUM(Amount / Divisor) FROM Journal WHERE UserID = %i AND BillID = %i AND IsPayment = -1", $gUser['ID'], $bill['ID']);

		// Output
		if ($myChargeAmount != 0 || $myContribAmount != 0) { 
			$firstFields = "<tr" . ($i++ % 2 ? " class='gray'" : "") . "'><td>{$bill['Date']}</td><td><a href='bill.php?id={$bill['ID']}' title='Click to open the bill'" . ($accessKey ? " accesskey='$accessKey'" : "") . ">" . indicateAccessKey($bill['Description'], $accessKey) . "</a></td>";

			if ($myChargeAmount != 0) {
				$color = $bill['IsRepayment'] ? "green" : "black";
				print $firstFields . "<td style='color: $color;'>" . ($myChargeAmount > 0 ? money_format(moneyFormat, $myChargeAmount) : "") . "</td><td style='color: $color;'>" . ($myChargeAmount < 0 ? money_format(moneyFormat, -$myChargeAmount) : "") . "</td></tr>";
			}
	
			if ($myContribAmount != 0) {
				$color = $bill['IsRepayment'] ? "green" : "blue";
				print $firstFields . "<td style='color: $color;'>" . ($myContribAmount < 0 ? -money_format(moneyFormat, $myContribAmount) : "") . "</td><td style='color: $color;'>" . ($myContribAmount > 0 ? money_format(moneyFormat, $myContribAmount) : "") . "</td></tr>";
			}
		}
	}
	print "</table>";

	print_footer();
?>
